package com.efast.cafe.portal.service.api.impl;

import java.util.List;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.dao.api.PortalApiServiceSampleDao;
import com.efast.cafe.portal.entity.api.PortalApiInputParam;
import com.efast.cafe.portal.entity.api.PortalApiOutputParam;
import com.efast.cafe.portal.entity.api.PortalApiServiceSample;
import com.efast.cafe.portal.service.api.IPortalApiServiceSampleService;
import com.efast.cafe.portal.service.api.IPortalApiServiceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * Service接口实现类
 * </p>
 * <p>
 * Table: portal_api_service_sample - 服务示例
 * </p>
 *
 * @since 2017-08-10 02:39:04
 */
@Service
@Transactional
public class PortalApiServiceSampleServiceImpl implements IPortalApiServiceSampleService {

	@Autowired
	private IPortalApiServiceService portalApiServiceService;
	
	@Autowired
	private PortalApiServiceSampleDao portalApiServiceSampleDao;

	

	@Override
	public void valideSampleParams(String reqDefine, String sampleReqDefine, String resDefine, String sampleResDefine) {
		if (resDefine == null || resDefine.isEmpty()) {
			throw new ServiceException("响应定义不能为空！");
		}
		if (sampleResDefine == null || sampleResDefine.isEmpty()) {
			throw new ServiceException("示例响应描述不能为空！");
		}
		List<PortalApiInputParam> reqParamList = portalApiServiceService.parseInputparams(reqDefine);
		List<PortalApiInputParam> sampleReqParamList = portalApiServiceService.parseInputparams(sampleReqDefine);
		// 验证示例请求描述
		valideSampleInputParams(reqParamList, sampleReqParamList);
		List<PortalApiOutputParam> resParamList = portalApiServiceService.parseOutputparams(resDefine);
		List<PortalApiOutputParam> sampleResParamList = portalApiServiceService.parseOutputparams(sampleResDefine);
		// 验证示例请求描述
		valideSampleOutputParams(resParamList, sampleResParamList);
	}

	/**
	 * 验证示例请求描述
	 * 
	 * @param reqParamList
	 * @param sampleReqParamList
	 *            void
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private void valideSampleInputParams(List<PortalApiInputParam> reqParamList,
			List<PortalApiInputParam> sampleReqParamList) {
		if (reqParamList == null && sampleReqParamList == null) {
			return;
		}
		if (reqParamList == null && sampleReqParamList != null) {
			throw new ServiceException("请求定义为空,示例请求描述应为空！");
		}
		// 验证请求描述中参数是否都存在
		for (PortalApiInputParam sampleReqParam : sampleReqParamList) {
			String paramName = sampleReqParam.getParamName();
			Byte paramType = sampleReqParam.getParamDataType();
			if (!valideInputParamIsExist(paramName, paramType, reqParamList)) {
				int index=paramName.lastIndexOf("children.");
				paramName = paramName.substring(index);
				throw new ServiceException("参数" + paramName + "," + "类型：" + paramType + "在请求定义中未定义");
			}
			;
		}
		// 验证是否缺少必要的请求参数
		valideInputParamNecessity(reqParamList,sampleReqParamList);
	}
	/**
	 * 验证示例请求描述
	 * 
	 * @param reqParamList
	 * @param sampleReqParamList
	 *            void
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private void valideSampleOutputParams(List<PortalApiOutputParam> resParamList,
			List<PortalApiOutputParam> sampleResParamList) {
		// 验证请求描述中参数是否都存在
		for (PortalApiOutputParam sampleResParam : sampleResParamList) {
			String paramName = sampleResParam.getParamName();
			Byte paramType = sampleResParam.getParamDataType();
			if (!valideOutputParamIsExist(paramName, paramType, resParamList)) {
				int index=paramName.lastIndexOf("children.");
				paramName = paramName.substring(index);
				throw new ServiceException("参数" + paramName + "," + "类型：" + paramType + "在响应定义中未定义");
			}
			;
		}
		// 验证是否缺少必要的请求参数
		valideOutputParamNecessity(resParamList,sampleResParamList);
	}

	/**
	 * 请求参数是否存在参数列表中
	 * 
	 * @param paramName 参数名
	 * @param paramType 参数类型
	 * @param reqParamList  参数列表
	 * @return boolean
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private boolean valideInputParamIsExist(String paramName, Byte paramType, List<PortalApiInputParam> reqParamList) {
		for (PortalApiInputParam reqParam : reqParamList) {
			if (reqParam.getParamName().equals(paramName)) {
				if (reqParam.getParamDataType()!=null && paramType!=null && 
						reqParam.getParamDataType().intValue()==paramType.intValue()) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 验证请求参数的是否缺失必要参数
	 * 
	 * @param reqParamList
	 * @param sampleReqParamList
	 *            void
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private void valideInputParamNecessity(List<PortalApiInputParam> reqParamList,
			List<PortalApiInputParam> sampleReqParamList) {
		for(PortalApiInputParam reqParam:reqParamList){
			if(reqParam.getRequired()==1){
				String name = reqParam.getParamName();
				Byte type = reqParam.getParamDataType();
				if(!valideInputParamIsExist(name,type,sampleReqParamList)){
					int index=name.lastIndexOf("children.");
					name = name.substring(index);
					throw new ServiceException("参数" + name + "," + "类型：" + type + "是必要参数，在请求示例中定义中未定义");
				}
			}
		}
	}
	
	/**
	 * 响应参数是否存在参数列表中
	 * 
	 * @param paramName 参数名
	 * @param paramType 参数类型
	 * @param resParamList  参数列表
	 * @return boolean
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private boolean valideOutputParamIsExist(String paramName, Byte paramType, List<PortalApiOutputParam> resParamList) {
		for (PortalApiOutputParam resParam : resParamList) {
			if (resParam.getParamName().equals(paramName)) {
				if (resParam.getParamDataType()!=null && null != paramType 
						&& resParam.getParamDataType().intValue()==paramType.intValue()) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 验证响应参数的是否缺失必要参数
	 * 
	 * @param resParamList
	 * @param sampleResParamList
	 *            void
	 * @Author Administrator
	 * @Date 2017年8月31日
	 */
	private void valideOutputParamNecessity(List<PortalApiOutputParam> resParamList,
			List<PortalApiOutputParam> sampleResParamList) {
		for(PortalApiOutputParam resParam:resParamList){
			if(resParam.getRequired()==1){
				String name = resParam.getParamName();
				Byte type = resParam.getParamDataType();
				if(!valideOutputParamIsExist(name,type,sampleResParamList)){
					int index=name.lastIndexOf("children.");
					name = name.substring(index);
					throw new ServiceException("参数" + name + "," + "类型：" + type + "是必要参数，在响应示例中定义中未定义");
				}
			}
		}
	}

	/**
	 * 查询详情
	 */
	@Override
	public PortalApiServiceSample detailUriSample(PortalApiServiceSample apiServiceSample) {
		// TODO Auto-generated method stub
		return portalApiServiceSampleDao.detailUriSample(apiServiceSample);
	}
}